﻿using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Linq;
using System.Transactions;
using System.Web;

namespace FTBS.Models.DAO
{
    public class ScheduleDAO
    {
        private FilmTicketBookingSystemEntities _entities = new FilmTicketBookingSystemEntities();
        //private SettingTheaterDAO _settingTheaterDao = new SettingTheaterDAO();
        private ShowTimeDAO _showTimeDao = new ShowTimeDAO();
        private TheaterDAO _theaterDao = new TheaterDAO();

        #region --- Booking Ticket ---

        /// <summary>
        /// Get list of schedule
        /// </summary>
        /// <returns>List of schedule</returns>
        /// <author>vuongvt</author>
        /// <date>2014/02/27</date>
        public IEnumerable<Schedule> GetListShedule()
        {
            try
            {
                IEnumerable<Schedule> listSchedule = new List<Schedule>();
                listSchedule = _entities.Schedules.ToList().Where(schedule => schedule.IsDeleted == false);
                return listSchedule;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Get list schedule by a film id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>nguyenht</author>
        /// <edit>VuongVT: 10/04/2014</edit>
        public List<Schedule> GetListScheduleById(int id)
        {
            try
            {
                List<Schedule> listSchedule = new List<Schedule>();
                var resultQuery = _entities.Schedules.Where(schedule => schedule.IsDeleted == false
                                                                        && schedule.FilmId == id
                                                                        && schedule.IsActive == true);
                foreach (var item in resultQuery)
                {
                    if (!isFullTicket(item))
                    {
                        if (!isSame(listSchedule, item))
                        {
                            listSchedule.Add(item);
                        }
                    }
                }
                return listSchedule;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get list of films are showing
        /// </summary>
        /// <returns>List of Film</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/2</date>
        public List<Film> GetListOfFilmShowing()
        {
            try
            {
                List<Film> listFilm = new List<Film>();

                //Condition for query
                //Schedule is not Deleted
                //Schedule created ticket in table Ticket -> schedule.IsCreateTicket == true
                //ShowTime of this Schedule is Active -> schedule.ShowTime.IsActive == true
                var listFilmResult = (from schedule in _entities.Schedules
                                      from film in _entities.Films
                                      where schedule.IsDeleted == false
                                            && schedule.IsCreateTicket == true
                                            && schedule.FilmId == film.FilmId
                                            && film.IsDeleted == false
                                            && schedule.IsActive == true
                                      select new
                                      {
                                          Film = film,
                                          Schedule = schedule
                                      });
                foreach (var item in listFilmResult)
                {
                    if (!IsFullTicketForSchedule(item.Schedule))
                    {
                        if (!listFilm.Contains(item.Film))
                        {
                            listFilm.Add(item.Film);
                        }
                    }
                }
                return listFilm;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Get unique film coming soon
        /// </summary>
        /// <returns>list film</returns>
        /// <author>nguyenht</author>
        public List<Film> GetListOfFilmComing()
        {
            try
            {
                List<Film> listFilm = new List<Film>();
                var listFilmResult = _entities.Schedules.Where(schedule => schedule.IsDeleted == false
                    //&& s.IsCreateTicket == false
                    && schedule.IsActive == false && schedule.ShowTime.IsDeleted == false).Select(s => s.Film);
                foreach (var item in listFilmResult)
                {
                    if (!listFilm.Contains(item))
                    {
                        listFilm.Add(item);
                    }
                }
                return listFilm;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Get List film has the same category
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>nguyenht</author>
        public List<Film> GetListOfFilmSameCate(int cateId, int id)
        {
            try
            {
                List<Film> listFilm = new List<Film>();
                var listFilmResult = _entities.Schedules.Where(s => s.IsDeleted == false
                    //&& s.IsCreateTicket == false
                     && s.ShowTime.IsDeleted == false && s.Film.CategoryId == cateId && s.FilmId != id).Select(s => s.Film);
                foreach (var item in listFilmResult)
                {
                    if (!listFilm.Contains(item))
                    {
                        listFilm.Add(item);
                    }
                }
                return listFilm;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<Promotion> GetListOListPromotion()
        {
            try
            {
                List<Promotion> listPromotion = new List<Promotion>();
                var listProResult = _entities.Schedules.Where(s => s.IsDeleted == false
                    && s.IsCreateTicket == true && s.IsActive == true && s.PromotionId!=null).Select(s => s.Promotion);
                foreach (var item in listProResult)
                {
                   
                        listPromotion.Add(item);
                   
                }
                return listPromotion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Check Theater created Ticket or not
        /// </summary>
        /// <param name="scheduleId"></param>
        /// <returns>True: Have Ticket ---- False: Not have ticket</returns>
        /// <author>VuongVT</author>
        public bool IsTheaterHaveTicket(int scheduleId)
        {
            try
            {
                var resultQuery = _entities.Tickets.Any(t => t.ScheduleId == scheduleId
                                                        && t.IsDeleted == false);
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get list theater of film by filmId
        /// -> Use when user click to a film. Load all theater are showing for this film
        /// </summary>
        /// <param name="filmId"></param>
        /// <returns></returns>
        public List<Theater> GetListTheaterWhenChooseFilm(int filmId)
        {
            try
            {
                List<Theater> listTheater = new List<Theater>();

                //Condition for query
                //Schedule have ticket in table Ticket -> Schedule.IsCreateTicket = true
                //ShowTime of Schedule is Active -> Schedule.ShowTime.IsActive = true
                var resultQuery = (from schedule in _entities.Schedules
                                   from theater in _entities.Theaters
                                   where schedule.FilmId == filmId
                                         && schedule.IsDeleted == false
                                         && schedule.IsCreateTicket == true
                                         && schedule.TheaterId == theater.TheaterId
                                         && theater.IsDeleted == false
                                         && schedule.IsActive == true
                                   select new
                                   {
                                       Theater = theater,
                                       Schedule = schedule
                                   });

                foreach (var item in resultQuery)
                {
                    if (!IsFullTicketForSchedule(item.Schedule))
                    {
                        if (!listTheater.Contains(item.Theater))
                        {
                            listTheater.Add(item.Theater);
                        }
                    }
                }
                return listTheater;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check Room is full ticket or not. It use for load showtime
        /// When room is full ticket, don't get room have showtime
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>false: Not full -- true: Full ticket</returns>
        public bool IsFullTicket(ShowTimeConcertEntity showtime)
        {

            var countTotalTicket = (from ticket in _entities.Tickets
                                    where ticket.ScheduleId == showtime.ScheduleId
                                          && ticket.IsDeleted == false
                                    select ticket).Count();
            var countTicketBooked = (from ticket in _entities.Tickets
                                     where ticket.ScheduleId == showtime.ScheduleId
                                           && ticket.Status == true
                                           && ticket.IsDeleted == false
                                     select ticket).Count();
            if (countTicketBooked < countTotalTicket)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check in ListShowTime already have ShowTimeId or not
        /// </summary>
        /// <param name="listShowTimeConcert"></param>
        /// <param name="showtimeConcert"></param>
        /// <returns>True: Contain ShowTime --- False: Not contain ShowTime</returns>
        public bool IsContainInListShowTime(List<ShowTimeConcertEntity> listShowTimeConcert, ShowTimeConcertEntity showtimeConcert)
        {
            foreach (var item in listShowTimeConcert)
            {
                if (item.ShowTimeId == showtimeConcert.ShowTimeId)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Get list showtime by showtimeid in schedule table
        /// </summary>
        /// <param name="showtimeId"></param>
        /// <returns></returns>
        public List<ShowTimeConcertEntity> GetListShowTimeWhenChooseTheater(int theaterId, int filmId)
        {
            try
            {
                List<ShowTimeConcertEntity> listShowTimeResult = new List<ShowTimeConcertEntity>();

                var resultQuery = from schedule in _entities.Schedules
                                  from showtime in _entities.ShowTimes
                                  from theater in _entities.Theaters
                                  where schedule.IsDeleted == false
                                        && schedule.FilmId == filmId
                                        && schedule.IsCreateTicket == true
                                        && schedule.ShowTimeId == showtime.ShowTimeId
                                        && schedule.IsActive == true
                                        && showtime.IsDeleted == false
                                        && schedule.TheaterId == theaterId
                                        && theater.TheaterId == theaterId
                                        && theater.IsDeleted == false
                                  select new
                                  {
                                      Showtime = showtime,
                                      RoomId = schedule.RoomId,
                                      ScheduleId = schedule.ScheduleId,
                                      ShowTimeId = schedule.ShowTimeId,
                                      Schedule = schedule
                                  };
                foreach (var item in resultQuery)
                {
                    ShowTimeConcertEntity showtimeConcert = new ShowTimeConcertEntity();
                    showtimeConcert.ScheduleId = item.ScheduleId;
                    showtimeConcert.RoomId = item.RoomId;
                    showtimeConcert.ShowTimeId = item.ShowTimeId;

                    showtimeConcert.ShowTimeId = item.Showtime.ShowTimeId;
                    showtimeConcert.Time = item.Showtime.Time;
                    showtimeConcert.DateOfSchedule = item.Schedule.Date;
                    showtimeConcert.TheaterId = item.Showtime.TheaterId;
                    showtimeConcert.IsDeleted = item.Showtime.IsDeleted;

                    if (!IsFullTicket(showtimeConcert))
                    {
                        if (!IsContainInListShowTime(listShowTimeResult, showtimeConcert))
                        {
                            listShowTimeResult.Add(showtimeConcert);
                        }
                    }
                }
                return listShowTimeResult.OrderBy(sh => sh.DateOfSchedule).ThenBy(sh => sh.Time).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get RoomId to load chair for booking
        /// </summary>
        /// <param name="searchSchedule"></param>
        /// <returns>RoomId</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/02</date>
        public int GetRoomIdForBooking(SearchSchedule searchSchedule)
        {
            try
            {
                var resultQuery = _entities.Schedules.Where(s => s.FilmId == searchSchedule.FilmId
                                                             && s.ShowTimeId == searchSchedule.ShowTimeId
                                                             && s.TheaterId == searchSchedule.TheaterId)
                                                 .Select(r => new
                                                 {
                                                     RoomId = r.RoomId
                                                 });
                return resultQuery.FirstOrDefault().RoomId;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Get ScheduleId base on FilmId, RoomId, ShowTimeId, TheaterId
        /// </summary>
        /// <param name="filmId"></param>
        /// <param name="roomId"></param>
        /// <param name="showtimeId"></param>
        /// <param name="theaterId"></param>
        /// <returns>ScheduleId</returns>
        /// <author>vuongvt</author>
        public int GetScheduleId(int filmId, int roomId, int showtimeId, int theaterId)
        {
            try
            {
                var resultQuery = _entities.Schedules.Where(s => s.FilmId == filmId
                                                             && s.RoomId == roomId
                                                             && s.ShowTimeId == showtimeId
                                                             && s.TheaterId == theaterId).FirstOrDefault();
                return resultQuery.ScheduleId;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get user's information by username
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public User GetUserInfoByUserName(string userName)
        {
            try
            {
                var resultQuery = (from user in _entities.Users
                                   where user.UserName == userName
                                         && user.IsActive == true
                                         && user.IsDeleted == false
                                   select user).FirstOrDefault();

                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get list Theater use for Search by Theater
        /// </summary>
        /// <returns>List of Theater</returns>
        /// <author>VuongVT</author>
        public List<Theater> GetListTheaterForSearchByTheater()
        {
            try
            {
                List<Theater> listTheater = new List<Theater>();

                //Condition for query
                //Schedule is not Deleted
                //Schedule created ticket in table Ticket -> schedule.IsCreateTicket == true
                //ShowTime of this Schedule is Active -> schedule.ShowTime.IsActive == true
                //Get by Theater
                var listFilmResult = from schedule in _entities.Schedules
                                     from theater in _entities.Theaters
                                     where schedule.IsDeleted == false
                                           && schedule.IsCreateTicket == true
                                           && schedule.TheaterId == theater.TheaterId
                                           && theater.IsDeleted == false
                                           && schedule.IsActive == true
                                     select theater;
                foreach (var item in listFilmResult)
                {
                    if (!listTheater.Contains(item) && GetListFilmWhenChooseTheater(item.TheaterId).Count > 0)
                    {

                        listTheater.Add(item);
                    }
                }
                return listTheater;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get list of Film when Choose Theater -> User for booking by Theater
        /// </summary>
        /// <param name="theaterId"></param>
        /// <returns></returns>
        public List<Film> GetListFilmWhenChooseTheater(int theaterId)
        {
            try
            {
                List<Film> listFilm = new List<Film>();

                //Condition for query
                //Schedule is not Deleted
                //Schedule created ticket in table Ticket -> schedule.IsCreateTicket == true
                //ShowTime of this Schedule is Active -> schedule.ShowTime.IsActive == true
                var listFilmResult = (from schedule in _entities.Schedules
                                      from film in _entities.Films
                                      where schedule.IsDeleted == false
                                            && schedule.TheaterId == theaterId
                                            && schedule.IsCreateTicket == true
                                            && film.FilmId == schedule.FilmId
                                            && film.IsDeleted == false
                                            && schedule.IsActive == true
                                      select new
                                      {
                                          Film = film,
                                          Schedule = schedule
                                      });

                foreach (var item in listFilmResult)
                {
                    if (!IsFullTicketForSchedule(item.Schedule))
                    {
                        if (!listFilm.Contains(item.Film))
                        {
                            listFilm.Add(item.Film);
                        }
                    }
                }
                return listFilm;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get list showtime by date, theaterId and FilmId
        /// </summary>
        /// <param name="showtimeId"></param>
        /// <returns></returns>
        public List<ShowTimeConcertEntity> GetListShowTimeFastBuy(int theaterId, int filmId, DateTime date)
        {
            try
            {
                List<ShowTimeConcertEntity> listShowTimeResult = new List<ShowTimeConcertEntity>();

                var resultQuery = from schedule in _entities.Schedules
                                  from showtime in _entities.ShowTimes
                                  where schedule.IsDeleted == false
                                        && schedule.FilmId == filmId
                                        && schedule.IsCreateTicket == true
                                        && schedule.IsActive == true
                                        && schedule.TheaterId == theaterId
                                        && schedule.Date == date
                                        && showtime.ShowTimeId == schedule.ShowTimeId
                                  select new
                                  {
                                      Time = showtime.Time,
                                      Date = schedule.Date,
                                      RoomId = schedule.RoomId,
                                      TheaterId = schedule.TheaterId,
                                      ScheduleId = schedule.ScheduleId,
                                      ShowTimeId = schedule.ShowTimeId,
                                  };
                foreach (var item in resultQuery)
                {
                    ShowTimeConcertEntity showtimeConcert = new ShowTimeConcertEntity();
                    showtimeConcert.ScheduleId = item.ScheduleId;
                    showtimeConcert.RoomId = item.RoomId;
                    showtimeConcert.ShowTimeId = item.ShowTimeId;

                    showtimeConcert.Time = item.Time;
                    showtimeConcert.DateOfSchedule = item.Date;
                    showtimeConcert.TheaterId = item.TheaterId;

                    if (!IsFullTicket(showtimeConcert))
                    {
                        if (!IsContainInListShowTime(listShowTimeResult, showtimeConcert))
                        {
                            listShowTimeResult.Add(showtimeConcert);
                        }
                    }
                }
                return listShowTimeResult.OrderBy(sh => sh.DateOfSchedule).ThenBy(sh => sh.Time).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get list showtime by date,showtimeId, theaterId and FilmId
        /// </summary>
        /// <param name="showtimeId"></param>
        /// <returns></returns>
        public List<ShowTimeConcertEntity> SetUpFastBuy(int theaterId, int filmId, DateTime date, int showtimeId)
        {
            try
            {
                List<ShowTimeConcertEntity> listShowTimeResult = new List<ShowTimeConcertEntity>();

                var resultQuery = from schedule in _entities.Schedules
                                  from showtime in _entities.ShowTimes
                                  where schedule.IsDeleted == false
                                        && schedule.FilmId == filmId
                                        && schedule.IsCreateTicket == true
                                        && schedule.IsActive == true
                                        && schedule.TheaterId == theaterId
                                        && schedule.Date == date
                                        && showtimeId == schedule.ShowTimeId
                                  select new
                                  {
                                      Time = showtime.Time,
                                      Date = schedule.Date,
                                      RoomId = schedule.RoomId,
                                      TheaterId = schedule.TheaterId,
                                      ScheduleId = schedule.ScheduleId,
                                      ShowTimeId = schedule.ShowTimeId,
                                  };
                foreach (var item in resultQuery)
                {
                    ShowTimeConcertEntity showtimeConcert = new ShowTimeConcertEntity();
                    showtimeConcert.ScheduleId = item.ScheduleId;
                    showtimeConcert.RoomId = item.RoomId;
                    showtimeConcert.ShowTimeId = item.ShowTimeId;

                    showtimeConcert.Time = item.Time;
                    showtimeConcert.DateOfSchedule = item.Date;
                    showtimeConcert.TheaterId = item.TheaterId;

                    if (!IsFullTicket(showtimeConcert))
                    {
                        if (!IsContainInListShowTime(listShowTimeResult, showtimeConcert))
                        {
                            listShowTimeResult.Add(showtimeConcert);
                        }
                    }
                }
                return listShowTimeResult.OrderBy(sh => sh.DateOfSchedule).ThenBy(sh => sh.Time).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region --- Manage Schedule ---

        /// <summary>
        /// Check is Schedule existed in Schedule table
        /// </summary>
        /// <param name="filmId"></param>
        /// <param name="roomId"></param>
        /// <param name="showtimeId"></param>
        /// <param name="theaterId"></param>
        /// <returns>true: Existed - false: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsExistSchedule(int filmId, int roomId, int showtimeId, int theaterId, DateTime scheduleDate)
        {
            try
            {
                var resultQuery = _entities.Schedules.Any(s => s.RoomId == roomId
                                                               && s.ShowTimeId == showtimeId
                                                               && s.TheaterId == theaterId
                                                               && s.Date == scheduleDate
                                                               && s.IsDeleted == false);
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check have exist Schedule use for Edit schedule
        /// </summary>
        /// <param name="scheduleId"></param>
        /// <param name="filmId"></param>
        /// <param name="roomId"></param>
        /// <param name="showtimeId"></param>
        /// <param name="theaterId"></param>
        /// <returns>true: Existed - false: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsExistScheduleForEdit(int scheduleId, int filmId, int roomId, int showtimeId, int theaterId, DateTime scheduleDate)
        {
            try
            {
                var resultQuery = _entities.Schedules.Any(s => s.ScheduleId != scheduleId
                                                               && s.RoomId == roomId
                                                               && s.ShowTimeId == showtimeId
                                                               && s.Date == scheduleDate
                                                               && s.TheaterId == theaterId);
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Is selected Promotion validate with Date of Schedule
        /// </summary>
        /// <param name="schedule"></param>
        /// <returns></returns>
        public bool IsValidatePromotion(Schedule schedule)
        {
            try
            {
                var queryPromotion = (from promotion in _entities.Promotions
                                      where promotion.PromotionId == schedule.PromotionId
                                            && promotion.IsDeleted == false
                                      select promotion).FirstOrDefault();
                var fromDate = queryPromotion.FromDate;
                var toDate = queryPromotion.ToDate;
                if (schedule.Date < fromDate)
                {
                    return false;
                }
                if (schedule.Date > toDate)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Get all schedule information to load to list schedule
        /// </summary>
        /// <returns>List of ScheduleConcertEntity</returns>
        public List<ScheduleConcertEntity> GetAllScheduleInfo()
        {
            try
            {
                List<ScheduleConcertEntity> listResult = new List<ScheduleConcertEntity>();

                var resultQuery = from schedule in _entities.Schedules
                                  join film in _entities.Films on schedule.FilmId equals film.FilmId
                                  join room in _entities.Rooms on schedule.RoomId equals room.RoomId
                                  join theater in _entities.Theaters on schedule.TheaterId equals theater.TheaterId
                                  join showtime in _entities.ShowTimes on schedule.ShowTimeId equals showtime.ShowTimeId
                                  join user in _entities.Users on schedule.UserName equals user.UserName
                                  join promotion in _entities.Promotions on schedule.PromotionId equals promotion.PromotionId into pro
                                  from promotion in pro.DefaultIfEmpty()
                                  where schedule.IsDeleted == false
                                  && schedule.Date >= DateTime.Today
                                  && film.IsDeleted == false
                                  && room.IsDeleted == false
                                  && theater.IsDeleted == false
                                  && showtime.IsDeleted == false
                                      //&& showtime.IsActive == true
                                      //&& schedule.Promotion.IsDeleted == false
                                  && user.IsActive == true
                                  && user.IsDeleted == false
                                  orderby schedule.Date descending
                                  select new
                                  {
                                      Schedules = schedule,
                                      FilmName = film.Name,
                                      RoomName = room.RoomName,
                                      Theater = theater.TheaterName,
                                      TimeForShow = showtime.Time,
                                      PromotionName = promotion == null ? "" : promotion.PromotionName,
                                      PromotionRate = promotion == null ? 0 : promotion.Rate,
                                      PeopleCreate = user.UserName,
                                      //IsShowing = schedule.IsActive
                                  };

                foreach (var item in resultQuery)
                {

                    Theater theater = new Theater();
                    theater = _theaterDao.GetTheaterById(item.Schedules.TheaterId);
                    System.TimeSpan duration = new System.TimeSpan(theater.DeadlineCreateTicket, 0, 0, 0);
                    if (item.Schedules.Date >= DateTime.Today.Add(duration))
                    {
                        ScheduleConcertEntity scheduleEntity = new ScheduleConcertEntity();
                        scheduleEntity.ScheduleId = item.Schedules.ScheduleId;
                        scheduleEntity.FilmId = item.Schedules.FilmId;
                        scheduleEntity.RoomId = item.Schedules.RoomId;
                        scheduleEntity.ShowTimeId = item.Schedules.ShowTimeId;
                        scheduleEntity.TheaterId = item.Schedules.TheaterId;
                        scheduleEntity.PromotionId = item.Schedules.PromotionId;
                        scheduleEntity.UserName = item.Schedules.UserName;
                        scheduleEntity.FilmName = item.FilmName;
                        scheduleEntity.RoomName = item.RoomName;
                        scheduleEntity.TheaterName = item.Theater;
                        scheduleEntity.Date = item.Schedules.Date;
                        scheduleEntity.TimeForShow = item.TimeForShow.ToString(@"hh\:mm");
                        if (item.PromotionName == null)
                        {
                            scheduleEntity.PromotionName = "";
                        }
                        else
                        {
                            scheduleEntity.PromotionName = item.PromotionName;
                        }
                        //if (item.PromotionRate == null)
                        //{
                        //    scheduleEntity.PromotionRate = 0;
                        //}
                        //else
                        //{
                        //    scheduleEntity.PromotionRate = Convert.ToDouble(item.PromotionRate);
                        //}
                        scheduleEntity.PeopleCreate = item.PeopleCreate;
                        scheduleEntity.IsActive = item.Schedules.IsActive;
                        scheduleEntity.IsCreateTicket = item.Schedules.IsCreateTicket;
                        listResult.Add(scheduleEntity);
                    }
                }
                return listResult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check Schedule have any ticket booked or not
        /// </summary>
        /// <param name="scheduleId"></param>
        /// <returns></returns>
        private bool IsScheduleHaveTicketBooked(int scheduleId)
        {
            try
            {
                var resultQuery = _entities.Tickets.Any(ticket => ticket.ScheduleId == scheduleId
                                                                  && ticket.IsDeleted == false
                                                                  && ticket.Status == true);
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Add new a schedule into Schedule table
        /// </summary>
        /// <param name="scheduleConcertEntity"></param>
        /// <returns>1: Success, 0: Fail</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/04</date>
        public int CreateSchedule(Schedule schedule, string userName)
        {
            try
            {
                schedule.UserName = userName;
                _entities.Schedules.Add(schedule);
                var resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Get schedule by ScheduleId
        /// </summary>
        /// <param name="scheduleId"></param>
        /// <returns></returns>
        public Schedule GetScheduleById(int scheduleId)
        {
            try
            {
                var resultQuery = (from schedule in _entities.Schedules
                                   where schedule.ScheduleId == scheduleId && schedule.IsDeleted == false
                                   select schedule).FirstOrDefault();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Edit a schedule. When edit schedule, update status of IsCreateTicket = false, staff have to create ticket
        /// Delete all old ticket which is created before update this schedule
        /// </summary>
        /// <param name="scheduleConcertEntity"></param>
        /// <returns>1: Success, 0: Fail</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/04</date>
        public int EditSchedule(Schedule schedule, string username, bool isChangeTheaterOrRoom)
        {
            try
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    try
                    {
                        var startingIssue = _entities.Schedules.Find(schedule.ScheduleId);
                        _entities.Detach(startingIssue);
                        schedule.UserName = username;
                        if (isChangeTheaterOrRoom)
                        {
                            schedule.IsCreateTicket = false;
                        }

                        _entities.Entry(schedule).State = System.Data.EntityState.Modified;
                        var resultUpdateSchedule = _entities.SaveChanges();
                        if (isChangeTheaterOrRoom)
                        {
                            if (resultUpdateSchedule != 0)
                            {

                                IEnumerable<Ticket> selectTicket = from ticket in _entities.Tickets
                                                                   where ticket.ScheduleId == schedule.ScheduleId
                                                                   select ticket;
                                foreach (Ticket ticket in selectTicket)
                                {
                                    _entities.Entry(ticket).State = System.Data.EntityState.Deleted;
                                }
                                var resultDelete = _entities.SaveChanges();
                            }
                        }

                        trans.Complete();
                        return 1;
                    }
                    catch (Exception ex)
                    {
                        trans.Dispose();
                        return 0;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Delete a schedule
        /// </summary>
        /// <param name="scheduleConcertEntity"></param>
        /// <returns>1: Success, 0: Fail</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/04</date>
        public int DeleteSchedule(Schedule schedule)
        {
            try
            {
                using (TransactionScope trans = new TransactionScope())
                {
                    try
                    {
                        schedule.IsDeleted = true;
                        _entities.Entry(schedule).State = EntityState.Modified;
                        var resultDeleteSchedule = _entities.SaveChanges();
                        //Delete all tickets of schedule which is deleted
                        if (resultDeleteSchedule == 1)
                        {
                            var ticketQuery = from ticket in _entities.Tickets
                                              where ticket.ScheduleId == schedule.ScheduleId
                                                    && ticket.IsDeleted == false
                                                    && ticket.Status == false
                                              select ticket;
                            foreach (var ticket in ticketQuery)
                            {
                                ticket.IsDeleted = false;
                                _entities.Entry(ticket).State = EntityState.Modified;
                            }
                            var resultDeleteTicket = _entities.SaveChanges();
                            return resultDeleteTicket;
                        }
                        else
                        {
                            return 0;
                        }
                    }
                    catch (Exception)
                    {
                        trans.Dispose();
                        return 0;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        /// <summary>
        /// Update IsCreateTicket = true when finish create Ticket
        /// </summary>
        /// <param name="schedule"></param>
        /// <returns></returns>
        /// <author>VuongVT</author>
        public int UpdateCreatedTicket(Schedule schedule)
        {
            try
            {
                schedule.IsCreateTicket = true;
                _entities.Entry(schedule).State = EntityState.Modified;
                var resultQuery = _entities.SaveChanges();
                return resultQuery;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check the Schedule is using  and have Booked ticket or not
        /// This function is used for user Click to Edit and Delete in List Schedule
        /// </summary>
        /// <param name="showTimeId"></param>
        /// <returns>True: Is Exist ---- False: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsScheduleUsingAndBooked(int scheduleId)
        {
            try
            {
                bool isUsedSchedule = false;

                var isUsing = _entities.Schedules.Any(schedule => schedule.ScheduleId == scheduleId
                                                                  && schedule.IsDeleted == false
                                                                  && schedule.IsCreateTicket == true);
                if (isUsing)
                {
                    var isBookedTicket = _entities.Tickets.Any(ticket => ticket.ScheduleId == scheduleId
                                                                     && ticket.IsDeleted == false
                                                                     && ticket.Status == true);
                    if (isBookedTicket)
                    {
                        isUsedSchedule = true;
                    }
                }
                return isUsedSchedule;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Check Date of Schedule is smaller than Deadline for create or not
        /// This function is used when staff create Schedule
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>True: Smaller --- False: Bigger</returns>
        public bool IsScheduleSmallerThanDeadline(Schedule schedule)
        {
            try
            {
                Theater theater = _theaterDao.GetTheaterById(schedule.TheaterId);
                System.TimeSpan duration = new System.TimeSpan(theater.DeadlineCreateTicket, 0, 0, 0);
                if (schedule.Date < DateTime.Today.Add(duration))
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region --LoadFilmOnHomePage--

        /// <summary>
        /// Check A film is exist in a List Film COncert Entity or not
        /// </summary>
        /// <param name="listFilm"></param>
        /// <param name="film"></param>
        /// <returns>True: Is Existed --- False: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsExistInListFilmConcertEntity(List<FilmConcertEntity> listFilm, FilmConcertEntity film)
        {
            foreach (var item in listFilm)
            {
                if (item.FilmId == film.FilmId)
                {
                    return true;
                }
            }
            return false;
        }


        /// <summary>
        /// LoadFilm in Comming soon
        /// </summary>
        /// <returns>ListFilmConcertEntity</returns>
        /// <author>VuongVT</author>
        /// <date>2014/03/06</date>
        public List<FilmConcertEntity> LoadFilmsCommingSoon()
        {
            try
            {
                List<FilmConcertEntity> listFilm = new List<FilmConcertEntity>();
                var resultQuery = _entities.Schedules.Where(s => s.IsActive == false
                                                            && s.ShowTime.IsDeleted == false
                                                            && s.IsDeleted == false)
                                                    .Select(r => new
                                                    {
                                                        Film = r.Film,
                                                        ScheduleId = r.ScheduleId,
                                                        TimeToShow = r.ShowTime.Time,
                                                        DateToShow = r.Date,
                                                        ShowTimeId = r.ShowTime.ShowTimeId,
                                                        TheaterId = r.TheaterId,
                                                    })
                                                    .Join(_entities.Theaters, schedule => schedule.TheaterId, theater => theater.TheaterId, (schedule, theater) => new { schedule, theater })
                                                    .Select(result => new
                                                    {
                                                        Film = result.schedule.Film,
                                                        ScheduleId = result.schedule.ScheduleId,
                                                        TimeToShow = result.schedule.TimeToShow,
                                                        DateToShow = result.schedule.DateToShow,
                                                        ShowTimeId = result.schedule.ShowTimeId,
                                                        TheaterId = result.theater.TheaterId,
                                                        TheaterName = result.theater.TheaterName
                                                    });

                foreach (var item in resultQuery)
                {
                    FilmConcertEntity filmConcertEntity = new FilmConcertEntity();
                    filmConcertEntity.FilmId = item.Film.FilmId;
                    filmConcertEntity.Name = item.Film.Name;
                    filmConcertEntity.Length = item.Film.Length;
                    filmConcertEntity.Nation = item.Film.Nation;
                    filmConcertEntity.Director = item.Film.Director;
                    filmConcertEntity.Actor = item.Film.Actor;
                    filmConcertEntity.AgeLimit = item.Film.AgeLimit;
                    filmConcertEntity.FormatId = item.Film.FormatId;
                    filmConcertEntity.CategoryId = item.Film.CategoryId;
                    filmConcertEntity.Description = item.Film.Description;
                    filmConcertEntity.ImageURL = item.Film.ImageURL;
                    filmConcertEntity.IsDeleted = item.Film.IsDeleted;
                    filmConcertEntity.ScheduleId = item.ScheduleId;
                    filmConcertEntity.TimeToShow = item.TimeToShow.ToString();
                    filmConcertEntity.DateToShow = item.DateToShow.ToString("dd/MM/yyyy");
                    filmConcertEntity.ShowTimeId = item.ShowTimeId;
                    filmConcertEntity.TheaterId = item.TheaterId;
                    filmConcertEntity.TheaterName = item.TheaterName;
                    filmConcertEntity.TrailerURL = item.Film.TrailerURL;
                    if (!IsExistInListFilmConcertEntity(listFilm, filmConcertEntity))
                    {
                        listFilm.Add(filmConcertEntity);
                    }
                }
                return listFilm;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Load Films are showing to Home page of guest and customer
        /// Load all film have show
        /// </summary>
        /// <returns>ListFilmConcertEntity</returns>
        /// <author>VuongVT</author>
        /// <date>2014/03/06</date>
        public List<Film> LoadFilmsAreShowing()
        {
            try
            {
                List<Film> listFilm = new List<Film>();
                var resultQuery = from schedule in _entities.Schedules
                                  from film in _entities.Films
                                  from showtime in _entities.ShowTimes
                                  where schedule.IsDeleted == false
                                        && schedule.IsCreateTicket == true
                                        && schedule.ShowTimeId == schedule.ShowTimeId
                                        && schedule.IsActive == true
                                        && showtime.IsDeleted == false
                                        && schedule.FilmId == film.FilmId
                                        && film.IsDeleted == false
                                  select film;
                foreach (var film in resultQuery)
                {
                    if (!listFilm.Contains(film))
                    {
                        listFilm.Add(film);
                    }
                }

                return listFilm;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Check Room is full ticket or not. It use for load showtime
        /// When room is full ticket, don't get room have showtime
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>false: Not full -- true: Full ticket</returns>
        public bool IsFullTicketForSchedule(Schedule schedule)
        {

            var countTotalTicket = (from ticket in _entities.Tickets
                                    where ticket.ScheduleId == schedule.ScheduleId
                                          && ticket.IsDeleted == false
                                    select ticket).Count();
            var countTicketBooked = (from ticket in _entities.Tickets
                                     where ticket.ScheduleId == schedule.ScheduleId
                                           && ticket.Status == true
                                           && ticket.IsDeleted == false
                                     select ticket).Count();
            if (countTicketBooked < countTotalTicket)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Check showtimeId contain in list schedule
        /// </summary>
        /// <param name="listSchedule"></param>
        /// <param name="schedule"></param>
        /// <returns></returns>
        public bool IsShowTimeContainInListSchedule(List<Schedule> listSchedule, Schedule schedule)
        {
            foreach (var item in listSchedule)
            {
                if (item.ShowTimeId == schedule.ShowTimeId)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Load Schedules of showing films
        /// </summary>
        /// <returns>List film</returns>
        /// <author>nguyenht</author>
        public List<Schedule> LoadSchedulesShowing()
        {
            try
            {
                List<Schedule> listSchedule = new List<Schedule>();

                //VuongVT: Viết lại kiểu Linq
                var resultQuery = from schedule in _entities.Schedules
                                  where schedule.Date >= DateTime.Today
                                        && schedule.IsActive == true
                                        && schedule.IsDeleted == false
                                        && schedule.IsCreateTicket == true
                                  select schedule;

                //var resultQuery = _entities.Schedules.Where(s => s.Date >= DateTime.Today)
                //                                     .Where(s => s.IsActive == true
                //                                         && s.IsDeleted==false
                //                                         && s.IsCreateTicket == true
                //                                            );
                foreach (var item in resultQuery)
                {
                    if (!isFullTicket(item))
                    {
                        if (!isSame(listSchedule, item))
                        {
                            listSchedule.Add(item);
                        }
                    }
                }

                return listSchedule.OrderBy(sh => sh.Date).ThenBy(sh => sh.ShowTime.Time).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public List<Schedule> LoadSchedulesPro()
        {
            try
            {
                List<Schedule> listSchedule = new List<Schedule>();

                //VuongVT: Viết lại kiểu Linq
                var resultQuery = from schedule in _entities.Schedules
                                  where schedule.Date >= DateTime.Today
                                        && schedule.IsActive == true
                                        && schedule.IsDeleted == false
                                        && schedule.IsCreateTicket == true
                                        && schedule.Promotion != null
                                  select schedule;
                foreach (var item in resultQuery)
                {
                    if (!isFullTicket(item))
                    {
                        if (!isSame(listSchedule, item))
                        {
                            listSchedule.Add(item);
                        }
                    }
                }

                return listSchedule.OrderBy(sh => sh.Date).ThenBy(sh => sh.ShowTime.Time).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// check if schedule is show in same film same showtime but different rooms
        /// </summary>
        /// <param name="slist"></param>
        /// <param name="schedule"></param>
        /// <returns></returns>
        public bool isSame(List<Schedule> slist, Schedule schedule)
        {
            if (slist.Any(s => s.ShowTimeId == schedule.ShowTimeId && s.FilmId == schedule.FilmId))
            {

                return true;
            }
            return false;
        }
        public bool isFullTicket(Schedule schedule)
        {
            if (schedule.Tickets.Where(s => s.Status == true || s.IsDeleted == true).Count() == (schedule.Tickets.Count())) { return true; }
            return false;
        }
        /// <summary>
        /// Load Schedule Sap Chieu
        /// </summary>
        /// <returns></returns>
        /// <author>nguyenht</author>
        public List<Schedule> LoadScheduleComing()
        {
            try
            {
                var listSchedule = new List<Schedule>();
                var resultQuery = from schedule in _entities.Schedules
                                  where schedule.Date >= DateTime.Today
                                        && schedule.IsActive == false
                                        && schedule.IsDeleted == false
                                  //&& schedule.IsCreateTicket == false
                                  select schedule;

                //var resultQuery = _entities.Schedules.Where(s => s.ShowTime.Date >= DateTime.Today)
                //                                     .Where(s => s.ShowTime.IsActive == false
                //                                         && s.ShowTime.IsDeleted == false
                //                                         && s.IsCreateTicket == false
                //                                         );

                foreach (var item in resultQuery) { listSchedule.Add(item); }
                return listSchedule;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Load Phim Duoc Mua Nhieu trong 7 ngày trước
        /// </summary>
        /// <returns></returns>
        /// <author>nguyenht</author>
        public List<Ticket> LoadScheduleHot()
        {
            try
            {
                List<Ticket> listTicket = new List<Ticket>();
                var ticketQuery = _entities.Tickets.Where(s => s.Schedule.IsActive == true
                                                                && EntityFunctions.AddDays(s.Schedule.Date, 7) > DateTime.Now
                                                                && s.Status == true
                                                                && s.IsDeleted == false);
                foreach (var item in ticketQuery)
                {
                    listTicket.Add(item);
                }
                return listTicket;
            }
            catch (Exception ex)
            { throw ex; }
        }
        /// <summary>
        /// Load phim khuyen mai. Promotion active && Showtime active
        /// </summary>
        /// <returns></returns>
        public List<FilmConcertEntity> LoadFilmsPromotion()
        {
            try
            {
                //Film have max rate in Promotion
                FilmConcertEntity filmBestPromotion = new FilmConcertEntity();
                filmBestPromotion = FilmBestPromotion();

                List<FilmConcertEntity> listFilmPromotion = new List<FilmConcertEntity>();
                var resultQuery = (from schedule in _entities.Schedules
                                   from showtime in _entities.ShowTimes
                                   from promotion in _entities.Promotions
                                   from theater in _entities.Theaters
                                   from film in _entities.Films
                                   where schedule.IsDeleted == false
                                         && schedule.IsCreateTicket == true
                                         && schedule.PromotionId != null
                                         && schedule.PromotionId == promotion.PromotionId
                                         && schedule.IsActive == true
                                         && promotion.IsDeleted == false
                                         && schedule.ShowTimeId == showtime.ShowTimeId
                                         && showtime.IsDeleted == false
                                         && theater.TheaterId == schedule.TheaterId
                                         && theater.IsDeleted == false
                                         && schedule.FilmId == film.FilmId
                                         && film.IsDeleted == false
                                   select new
                                   {
                                       Film = film,
                                       Schedule = schedule,
                                       PromotionDescription = promotion.PromotionName,
                                       TheaterName = theater.TheaterName
                                   });
                foreach (var item in resultQuery)
                {

                    FilmConcertEntity filmConcertEntity = new FilmConcertEntity();
                    filmConcertEntity.FilmId = item.Film.FilmId;
                    filmConcertEntity.Name = item.Film.Name;
                    filmConcertEntity.Length = item.Film.Length;
                    filmConcertEntity.Nation = item.Film.Nation;
                    filmConcertEntity.Director = item.Film.Director;
                    filmConcertEntity.Actor = item.Film.Actor;
                    filmConcertEntity.AgeLimit = item.Film.AgeLimit;
                    filmConcertEntity.FormatId = item.Film.FormatId;
                    filmConcertEntity.CategoryId = item.Film.CategoryId;
                    filmConcertEntity.Description = item.Film.Description;
                    filmConcertEntity.ImageURL = item.Film.ImageURL;
                    filmConcertEntity.IsDeleted = item.Film.IsDeleted;
                    filmConcertEntity.PromotionDescription = item.PromotionDescription;
                    filmConcertEntity.ScheduleId = item.Schedule.ScheduleId;
                    filmConcertEntity.TheaterName = item.TheaterName;
                    filmConcertEntity.TrailerURL = item.Film.TrailerURL;
                    if (!listFilmPromotion.Contains(filmConcertEntity))
                    {
                        listFilmPromotion.Add(filmConcertEntity);
                    }
                }
                return listFilmPromotion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Max rate promotion in Schedule
        /// </summary>
        /// <returns></returns>
        public FilmConcertEntity FilmBestPromotion()
        {
            try
            {
                FilmConcertEntity filmBestPromotion = new FilmConcertEntity();

                var resultQuery = (from schedule in _entities.Schedules
                                   where schedule.IsDeleted == false
                                         && schedule.IsActive == true
                                         && schedule.ShowTime.IsDeleted == false
                                         && schedule.Promotion.IsDeleted == false
                                   orderby schedule.Promotion.Rate descending
                                   select new
                                   {
                                       ScheduleDescription = schedule.Promotion.PromotionName,
                                       ScheduleId = schedule.ScheduleId,
                                       Film = schedule.Film
                                   }).FirstOrDefault();
                if (resultQuery != null)
                {
                    if (resultQuery.Film != null && resultQuery.ScheduleDescription != null)
                    {
                        filmBestPromotion.FilmId = resultQuery.Film.FilmId;
                        filmBestPromotion.Name = resultQuery.Film.Name;
                        filmBestPromotion.Length = resultQuery.Film.Length;
                        filmBestPromotion.Nation = resultQuery.Film.Nation;
                        filmBestPromotion.Director = resultQuery.Film.Director;
                        filmBestPromotion.Actor = resultQuery.Film.Actor;
                        filmBestPromotion.AgeLimit = resultQuery.Film.AgeLimit;
                        filmBestPromotion.FormatId = resultQuery.Film.FormatId;
                        filmBestPromotion.CategoryId = resultQuery.Film.CategoryId;
                        filmBestPromotion.Description = resultQuery.Film.Description;
                        filmBestPromotion.ImageURL = resultQuery.Film.ImageURL;
                        filmBestPromotion.IsDeleted = resultQuery.Film.IsDeleted;
                        filmBestPromotion.PromotionDescription = resultQuery.ScheduleDescription;
                        filmBestPromotion.ScheduleId = resultQuery.ScheduleId;
                        filmBestPromotion.TrailerURL = resultQuery.Film.TrailerURL;
                    }
                }
                return filmBestPromotion;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public List<ShowTimeConcertEntity> GetListShowTime()
        //{
        //    try
        //    {
        //        List<ShowTimeConcertEntity> listShowTimeResult = new List<ShowTimeConcertEntity>();

        //        var resultQuery = from schedule in _entities.Schedules
        //                          from showtime in _entities.ShowTimes
        //                          where schedule.IsDeleted == false
        //                                && schedule.IsCreateTicket == true
        //                                && schedule.ShowTimeId == showtime.ShowTimeId
        //                                && schedule.IsActive == true
        //                                && showtime.IsDeleted == false
        //                          select new
        //                          {
        //                              Showtime = showtime,
        //                              RoomId = schedule.RoomId,
        //                              ShowTimeId = schedule.ShowTimeId,
        //                              Schedule = schedule
        //                          };
        //        foreach (var item in resultQuery)
        //        {
        //            ShowTimeConcertEntity showtimeConcert = new ShowTimeConcertEntity();
        //            showtimeConcert.ScheduleId = item.Schedule.ScheduleId;
        //            showtimeConcert.RoomId = item.RoomId;
        //            showtimeConcert.ShowTimeId = item.ShowTimeId;

        //            showtimeConcert.ShowTimeId = item.Showtime.ShowTimeId;
        //            showtimeConcert.Time = item.Showtime.Time;
        //            showtimeConcert.DateOfSchedule = item.Schedule.Date;
        //            showtimeConcert.TheaterId = item.Showtime.TheaterId;
        //            showtimeConcert.IsActive = item.Schedule.IsActive;
        //            showtimeConcert.IsDeleted = item.Showtime.IsDeleted;

        //            if (!IsFullTicket(showtimeConcert))
        //            {
        //                if (!IsContainInListShowTime(listShowTimeResult, showtimeConcert))
        //                {
        //                    listShowTimeResult.Add(showtimeConcert);
        //                }
        //            }
        //        }
        //        return listShowTimeResult;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}
        #endregion

        #region promotion
        /// <summary>
        /// select all schedules has promotionId
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>  
        /// <author>DUYHDV</author>
        /// <date>2014/03/29</date>
        public List<Promotion> GetCurrentPromotion()
        {
            try
            {
                List<Promotion> listPromotion = new List<Promotion>();
                var listPromotionResult = from promotion in _entities.Promotions
                                          where promotion.ToDate >= DateTime.Today
                                                && promotion.IsDeleted == false
                                          select promotion;
                foreach (var item in listPromotionResult)
                {
                    if (!listPromotion.Contains(item))
                    {
                        listPromotion.Add(item);
                    }
                }
                return listPromotion;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}